package com.wanpinghui.supplier.object;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.object.adapter.ObjectFilterAdapter;
import com.wanpinghui.wphlibrary.model.object.bean.FilterData;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/11/24
 ***/

public class FilterManager {
    private Activity context;
    private List<FilterData> locationList;
    private List<FilterData> typeList;
    private List<FilterData> otherList;
    private OnSelectListener selectListener;

    FilterManager(Activity context) {
        this.context = context;
        locationList = new ArrayList<>();
        typeList = new ArrayList<>();
        otherList = new ArrayList<>();
        locationList.add(new FilterData("全国", "all", true));
        locationList.add(new FilterData("我关注的区域","mine",FilterData.TYPE_SETTING, false));
        locationList.add(new FilterData("指定城市", "", FilterData.TYPE_ARROW, false));

        typeList.add(new FilterData("全部类型", "all", true));
        typeList.add(new FilterData("我关注的类型", "mine", FilterData.TYPE_SETTING, false));
        typeList.add(new FilterData("安装", "1"));
        typeList.add(new FilterData("维修", "2"));
        typeList.add(new FilterData("租赁", "3"));
        typeList.add(new FilterData("采购", "4"));

        otherList.add(new FilterData("全部订单", "all", true));
        otherList.add(new FilterData("进行中的", "ongoing"));
        otherList.add(new FilterData("我的订单", "mine"));
        otherList.add(new FilterData("免费订单", "free"));
    }

    void setSelectListener(OnSelectListener listener) {
        selectListener = listener;
    }

    void showFilter(View parentView) {
        View view = View.inflate(context, R.layout.view_popup_object_filter, null);
        ListView listView = (ListView) view.findViewById(R.id.lv_data);
        final TextView textViewMyAtten = (TextView) view.findViewById(R.id.tv_my_atten);
        final TextView textViewMyAttenType = (TextView) view.findViewById(R.id.tv_my_atten_type);
        final TextView textViewOrderType = (TextView) view.findViewById(R.id.tv_order_type);
        final View line1 = view.findViewById(R.id.view_line1);
        final View line2 = view.findViewById(R.id.view_line2);
        final View line3 = view.findViewById(R.id.view_line3);
        final ObjectFilterAdapter adapter = new ObjectFilterAdapter(context);
        final YunShlPopupWinow popupWinow = new YunShlPopupWinow(view, ViewGroup.LayoutParams.MATCH_PARENT, (int) (DensityUtil.getScreenHeight() * 0.5));
        popupWinow.setCommonConfig(context, YunShlPopupWinow.AnimaDirect.DIRECT_BOTTOM_TOP);
        listView.setAdapter(adapter);
        textViewMyAtten.setSelected(true);
        adapter.setDatas(locationList);
        adapter.setListener(new ObjectFilterAdapter.SettingListener() {
            @Override
            public void goSetting(int type) {
                if (selectListener != null) {
                    if(type==0)
                        selectListener.setMyCity();
                    else
                        selectListener.setMyAttenType();
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });
        textViewMyAtten.setSelected(true);
        textViewMyAtten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewMyAtten.setSelected(true);
                textViewOrderType.setSelected(false);
                textViewMyAttenType.setSelected(false);
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.GONE);
                line3.setVisibility(View.GONE);
                adapter.setDatas(locationList);
            }
        });

        textViewMyAttenType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewMyAtten.setSelected(false);
                textViewOrderType.setSelected(false);
                textViewMyAttenType.setSelected(true);
                line1.setVisibility(View.GONE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.GONE);
                adapter.setDatas(typeList);
            }
        });

        textViewOrderType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewMyAtten.setSelected(false);
                textViewOrderType.setSelected(true);
                textViewMyAttenType.setSelected(false);
                line1.setVisibility(View.GONE);
                line2.setVisibility(View.GONE);
                line3.setVisibility(View.VISIBLE);
                adapter.setDatas(otherList);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (selectListener != null) {
                    if (textViewMyAtten.isSelected()) {
                        if (locationList.get(position).getType() == FilterData.TYPE_ARROW) {
                            selectListener.selectCity();
                        } else {
                            textViewMyAtten.setText(locationList.get(position).getName());
                            selectListener.myAtten(locationList.get(position).getValue());
                        }
                    } else if (textViewMyAttenType.isSelected()) {
                        textViewMyAttenType.setText(typeList.get(position).getName());
                        selectListener.myAttenType(typeList.get(position).getValue());
                    } else if (textViewOrderType.isSelected()) {
                        textViewOrderType.setText(otherList.get(position).getName());
                        selectListener.orderType(otherList.get(position).getValue());
                    }
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });
        popupWinow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    interface OnSelectListener {
        void myAtten(String value);

        void myAttenType(String value);

        void orderType(String value);

        void setMyAttenType();

        void selectCity();

        void setMyCity();
    }
}
